package com.zzyl.serve.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.StringUtils;
import com.zzyl.nursing.dto.CancelTaskDto;
import com.zzyl.nursing.dto.ExecuteTaskDto;
import com.zzyl.nursing.dto.UpdateTaskTimeDto;
import com.zzyl.nursing.vo.NursingTaskByIdVo;
import com.zzyl.serve.domain.NursingTaskDto;
import com.zzyl.serve.domain.NursingTaskVo;
import com.zzyl.serve.service.INursingFvkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;

/**
 * TODO
 *
 * @Date 2025/10/29 16:16
 */
@RestController
@RequestMapping("/nursing/nursingTask")
public class NursingFvkController extends BaseController {

    @Autowired
    private INursingFvkService nursingFvkService;

    /**
     * 2.2.1 查询护理任务列表
     * @param dto
     * @return
     */
/*    @GetMapping("/list")
    public TableDataInfo getNursingFvkTableData(QueryNursingTaskfvkDto dto) {
        startPage();
        List<NursingTask> list = nursingFvkService.getNursingFvkTableData(dto);
        return getDataTable(list);
    }*/


    @GetMapping("/list")
    public TableDataInfo list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long nurseId, // 前端传的是 nurseId
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String  elderName,
            @RequestParam(required = false) Long projectId  ) {

        startPage();

        NursingTaskDto dto = new NursingTaskDto();
        // 字段名映射：前端 nurseId → 后端 nursingId（但注意类型！）
        dto.setNursingId(nurseId); // 先保留 Long，后面再说

        dto.setStatus(status);
        dto.setElderName(elderName); // 如果有其他条件可设
        dto.setProjectId(projectId);

        // 手动解析时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

// 处理 startTime
        if (StringUtils.hasText(startTime)) {
            LocalDateTime start = LocalDateTime.parse(startTime, formatter);
            if (dto.getParams() == null) {
                dto.setParams(new HashMap<>()); // 初始化 params
            }
            dto.getParams().put("startTime", start);
        }

// 处理 endTime
        if (StringUtils.hasText(endTime)) {
            LocalDateTime end = LocalDateTime.parse(endTime, formatter);
            if (dto.getParams() == null) {
                dto.setParams(new HashMap<>());
            }
            dto.getParams().put("endTime", end);
        }

        List<NursingTaskVo> list = nursingFvkService.selectNursingTaskList(dto);
        return getDataTable(list);
    }





    /**
     * 查询护理任务详细信息
     */
    @GetMapping("/{id}")
    public AjaxResult getDetailDataById(@PathVariable("id") Integer id) {
        NursingTaskByIdVo nursingTaskByIdVo = nursingFvkService.getDetailDataById(id);
        return AjaxResult.success(nursingTaskByIdVo);
    }





    /**
     * 取消任务
     * @return
     */
    @PutMapping("/cancel")
    public AjaxResult cancelTaskById(@RequestBody CancelTaskDto dto){
        nursingFvkService.cancelTaskById(dto);
        return AjaxResult.success();
    }
    /**
     * 任务执行
     */
    @PutMapping("/do")
    public AjaxResult executeTask(@RequestBody ExecuteTaskDto dto){
        nursingFvkService.executeTask(dto);
        return AjaxResult.success();
    }
    /**
     * 任务改时间
     */
    @PutMapping("/updateTime")
    public AjaxResult updateTaskTime(@RequestBody UpdateTaskTimeDto dto){
        nursingFvkService.updateTaskTime(dto);
        return AjaxResult.success();
    }


}

